<?php

/*
=====================================================
 FluxBB - Delux Board Converstations (DBC)
-----------------------------------------------------
 http://dbc-forum.com/
 http://fluxbb.org/
-----------------------------------------------------
 Copyright (c) 2010,2011 FluxBB - Delux Board Conversations (DBC)
-----------------------------------------------------
 Предсловие: Данный скрипт (DBC) не является работой с нуля, то 
 есть ядром скрипта всё также остаётся FuxBB,но теперь в оболочке
 DBC.
=====================================================
*/

// Make sure no one attempts to run this script "directly"
if (!defined('DBC'))
	exit;

// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('DBC_PLUGIN_LOADED', 1);

require DBC_ROOT.'language/'.$admin_language.'/pages.php';

//===========================================================================//
//= Start the script code =//

$action = isset($_GET['action']) ? $_GET['action'] : NULL;
$page_id = isset($_GET['id']) ? intval($_GET['id']) : NULL;

$page_head['bbcode'] = '<script type="text/javascript" src="engine/classes/js/post.js"></script>';

//===========================================================================//
//= Add Action page =//
if($action == 'add')
{
	//if your saving the page
	if(isset($_POST['form_sent']))
	{
		// Check to see if the Title, Body, Month, and Day were sent
		if(empty($_POST['title']))
			message('You need to enter a title for your page');
		elseif(empty($_POST['content']))
			message('You need to enter content for your page');
		
		// Clean up body and title from POST
		$title = DBC_trim($_POST['title']);
		$content = DBC_linebreaks(addslashes($_POST['content']));
		
		//insert the page in the database
		$db->query('INSERT INTO '.$db->prefix.'pages (title, content) VALUES("'.$title.'", "'.$content.'")') or error('Unable to save page', __FILE__, __LINE__, $db->error());
		$page_id = $db->insert_id();

		redirect('page.php?id='.$page_id, ''.$lang_pages['Done'].' &hellip;');
	}
	
	// Display the admin navigation menu
	generate_admin_menu($plugin);

?>
	<div class="blockform">

	<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_pages['Add Title'] ?></span></td>
	<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></table>
	
	<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" width="100%"><div class="forum_adv_c">

			<form id="example" method="post" action="admin_loader.php?plugin=<?php echo $plugin ?>&amp;action=<?php echo $action ?>">
				<input type="hidden" name="form_sent" value="TRUE" />
				<div class="inform">
					<fieldset>
						<legend><?php echo $lang_pages['Setting'] ?></legend>
						<div class="infldset">
							<div width="98%" id="bbcode_bar"></div>
							<table class="aligntop" cellspacing="0">
								<tr>
									<td><b><?php echo $lang_pages['Page Title'] ?></b> <br/> <input class="input_text" type="text" name="title" size="50" tabindex="1" /></td>
								</tr>
								<tr>
									<td><b><?php echo $lang_pages['Page Content'] ?></b> <?php echo $lang_pages['Page Content desc'] ?><textarea class="input_text" name="content" tabindex="2" rows="20" cols="50" style="width:100%"></textarea></td>
								</tr>
							</table>
						</div>
					</fieldset><br/>
				<p><input class="fbutton" type="submit" name="submit" value="<?php echo $lang_pages['Submit'] ?>" tabindex="3" /> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
				</div>
			</form>
	<br/>
	</div></td>
	<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</td></tr></table>
	<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></thead></table>

	</div>
<?php
}

//===========================================================================//
//= Edit Action page =//
elseif($action == 'edit')
{
	//if your saving the page
	if(isset($_POST['form_sent']))
	{
		// Check to see if the Title, Body, Month, and Day were sent
		if(empty($_POST['title']))
			message('You need to enter a title for your page');
		elseif(empty($_POST['content']))
			message('You need to enter content for your page');
			
		// Clean up body and title from POST
		$title = DBC_trim($_POST['title']);
		$content = DBC_linebreaks(addslashes($_POST['content']));
		
		$db->query('UPDATE '.$db->prefix.'pages SET title="'.$title.'", content="'.$content.'" WHERE id='.$page_id) or error('Unable to update page', __FILE__, __LINE__, $db->error());
				
		redirect('admin_loader.php?plugin='.$plugin, 'Page Sucessfully edited, Redirecting &hellip;');
	}

	//pull out page info
	$result = $db->query("SELECT id, title, content FROM ".$db_prefix."pages WHERE id='".intval($_GET['id'])."'") or error('Unable to fetch page information', __FILE__, __LINE__, $db->error());
	if (!$db->num_rows($result))
		message($lang_common['Bad request']);
		
	$data = $db->fetch_assoc($result);
	
	// Display the admin navigation menu
	generate_admin_menu($plugin);

?>
	<div class="blockform">

	<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_pages['Edit Title'] ?></span></td>
	<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></table>
	
	<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" width="100%"><div class="forum_adv_c">

			<form id="example" method="post" action="admin_loader.php?plugin=<?php echo $plugin ?>&amp;action=<?php echo $action ?>&amp;id=<?php echo $page_id ?>">
				<input type="hidden" name="form_sent" value="TRUE" />
				<div class="inform">
					<fieldset>
						<legend><?php echo $lang_pages['Setting'] ?></legend>
						<div class="infldset">
							<table class="aligntop" cellspacing="0">
								<tr>
									<td><b><?php echo $lang_pages['Page Title'] ?></b> <br/> <input  class="input_text"type="text" name="title" value="<?php echo $data['title']?>" size="25" tabindex="1" /></td>
								</tr>
								<tr>
									<td><b><?php echo $lang_pages['Page Content'] ?></b><textarea class="input_text" name="content" tabindex="2" rows="20" cols="50" style="width:100%"><?php echo DBC_htmlspecialchars($data['content'])?></textarea></td>
								</tr>
							</table>
						</div>
					</fieldset><br/>
					<p><input class="fbutton" type="submit" name="submit" value="<?php echo $lang_pages['Submit'] ?>" tabindex="3" /> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
				</div>
			</form>
	<br/>
	</div></td>
	<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</td></tr></table>
	<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></thead></table>

	</div>
<?php
}
//===========================================================================//
//= Delete Action page =//
elseif($action == 'delete')
{
	if(isset($_POST['delete_comply']))
	{
		//There isnt anything to do but delete the post and redirect
		$db->query('DELETE FROM '.$db->prefix.'pages WHERE id='.$page_id) or error('Unable to delete page', __FILE__, __LINE__, $db->error());
		
		redirect('admin_loader.php?plugin='.$plugin, 'Page Sucessfully Deleted, Redirecting &hellip;');
	}
		
	//Display the admin navigation menu
	generate_admin_menu($plugin);
?>
	<div class="blockform">

	<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_pages['Delete Title'] ?></span></td>
	<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></table>
	
	<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" width="100%"><div class="forum_adv_c">

			<form id="example" method="post" action="admin_loader.php?plugin=<?php echo $plugin ?>&amp;action=<?php echo $action?>&amp;id=<?php echo $page_id ?>">
				<div class="inform">
					<fieldset>
						<legend><?php echo $lang_pages['Important'] ?></legend>
						<div class="infldset">
							<p><?php echo $lang_pages['Confirmation'] ?></p>
							<p class="warntext"><strong><?php echo $lang_pages['Warning'] ?></strong></p>								
						</div>
					</fieldset><br/>
				<p><input class="fbutton" type="submit" name="delete_comply" value="<?php echo $lang_pages['Delete'] ?>" /> <a href="javascript:history.go(-1)"><?php echo $lang_common['Go back'] ?></a></p>
				</div>
			</form>
	<br/>
	</div></td>
	<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</td></tr></table>
	<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></thead></table>

	</div>

<?php
}

//===========================================================================//
//= Selection page =//
else
{	
	//Display the admin navigation menu
	generate_admin_menu($plugin);
?>
	<div class="blockform">

	<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_pages['Custom Pages'] ?></span></td>
	<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></table>
	
	<table class="forum_adv_table"  width="100%" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" class="forum_adv_tleft" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" width="100%"><div class="forum_adv_c">

			<div class="fakeform">
				<div class="inform">
					<fieldset>
					<legend><?php echo $lang_pages['Existing pages'] ?></legend>
						<div class="infldset">
							<table cellspacing="0">
<?php
	//get all pages info from the DB
	$result = $db->query('SELECT id, title, content FROM '.$db->prefix.'pages') or error('Unable to select pages from database', __FILE__, __LINE__, $db->error());
	if ($db->num_rows($result))
	{
		while($data = $db->fetch_assoc($result))
			echo "\t\t\t\t\t\t\t".'<tr>'."\n\t\t\t\t\t\t\t\t".'<th scope="row">'."\n\t\t\t\t\t\t\t\t\t".'<a href="admin_loader.php?plugin='.$plugin.'&amp;action=delete&amp;id='.$data['id'].'">'.$lang_pages['Delete'].'</a> | '."\n\t\t\t\t\t\t\t\t\t".'<a href="admin_loader.php?plugin='.$plugin.'&amp;action=edit&amp;id='.$data['id'].'">'.$lang_pages['Edit'].'</a>'."\n\t\t\t\t\t\t\t\t".'</th>'."\n\t\t\t\t\t\t\t\t".'<td><a href="page.php?id='.$data['id'].'">'.$data['title'].'</a></td>'."\n\t\t\t\t\t\t\t".'</tr>'."\n";
	}
	else
		echo "\t\t\t\t\t\t\t".'<tr>'."\n\t\t\t\t\t\t\t\t".'<th scope="row">'.$lang_pages['Not Pages'].'</td>'."\n\t\t\t\t\t\t\t".'</tr>'."\n";
?>
							</table>
						</div>
					</fieldset>
					<p><a href="admin_loader.php?plugin=<?php echo $plugin ?>&amp;action=add"><?php echo $lang_pages['Add Page'] ?></p>
				</div>
			</div>
	<br/>
	</div></td>
	<td align="left" class="forum_adv_tright" width="2"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</td></tr></table>
	<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
	<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
	<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
	</tr></thead></table>

	</div>

<?php	
}

// Note that the script just ends here. The footer will be included by admin_loader.php.